﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using r2mp_DataStructures;
using System.Diagnostics;

namespace r2mp_Excel
{
    public partial class Dialog : Form
    {
        //These 2 delegates are used for the communication between the excel part and the main part of PMP.
        public delegate void DrawMTA(MTA mta);
        public delegate void DrawPSP(Task psp);
        private int editIndex = -1;

        private DrawMTA drawMTA;

        public DrawMTA SetDrawMTA
        {
            set { drawMTA = value; }
        }

        private DrawPSP drawPSP;

        public DrawPSP SetDrawPSP
        {
            set { drawPSP = value; }
        }

        private Size file = new Size(330, 250);
        private Size config = new Size(670, 425);
        private Extractor ext = new Extractor();

        public Dialog()
        {
            InitializeComponent();
            Open();
        }

        private void tbPath_TextChanged(object sender, EventArgs e)
        {

        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex == 0)
            {
                this.Size = file;
                this.MaximumSize = file;
                this.MinimumSize = file;
            }
            else
            {
                this.Size = config;
                this.MaximumSize = config;
                this.MinimumSize = config;
            }
            Refresh();
        }

        private void rbMTA_CheckedChanged(object sender, EventArgs e)
        {
            changeCheck();
        }

        private void changeCheck()
        {
            if (rbPSP.Checked)
            {
                gbPeriod.Enabled = false;
            }
            else
            {
                gbPeriod.Enabled = true;
            }
        }

        private void rbPSP_CheckedChanged(object sender, EventArgs e)
        {
            changeCheck();
        }

        private void Dialog_Load(object sender, EventArgs e)
        {
            
        }

        private void AddMTA(string item)
        {
            lbMTA.Items.Add(item);
        }

        private void AddPSP(string item)
        {
            lbPSP.Items.Add(item);
        }

        private void EditMTA(string item)
        {
            lbMTA.Items[editIndex] = item;
        }

        private void EditPSP(string item)
        {
            lbPSP.Items[editIndex] = item;
        }

        private void bMTAAdd_Click(object sender, EventArgs e)
        {
            AddDialog dia = new AddDialog(AddDialog.MTA);
            dia.Add = AddMTA;
            dia.ShowDialog();
            Save();
        }

        private void bPSPAdd_Click(object sender, EventArgs e)
        {
            AddDialog dia = new AddDialog(AddDialog.PSP);
            dia.Add = AddPSP;
            dia.ShowDialog();
            Save();
        }

        private void bPSPDelete_Click(object sender, EventArgs e)
        {
            lbPSP.Items.RemoveAt(lbPSP.SelectedIndex);
            Save();
            if (lbPSP.Items.Count == 0)
            {
                bPSPDelete.Enabled = false;
                bPSPEdit.Enabled = false;
            }
        }

        /// <summary>
        /// This methode saves the items of the 2 lists into a file ("pattern.rin"). 
        /// If it doesn't exist it will be created
        /// </summary>
        private void Save()
        {
            try
            {
                Stream stream = File.Open("pattern.rin", FileMode.Create);
                BinaryFormatter bFormatter = new BinaryFormatter();
                List<string> temp = new List<string>();
                foreach (string now in lbMTA.Items)
                {
                    temp.Add(now);
                }
                bFormatter.Serialize(stream, temp.ToArray());
                temp = new List<string>();
                foreach (string now in lbPSP.Items)
                {
                    temp.Add(now);
                }
                bFormatter.Serialize(stream, temp.ToArray());
                stream.Close();
            }
            catch (IOException io)
            {
                MessageBox.Show("Ein unbekannter Fehler beim Lesen oder Schreiben ist aufgetreten.\n" +
                    "Wenden Sie sich bitte an Ihren Administrator","Schreibe-/Lese-Fehler",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
            catch (UnauthorizedAccessException ua)
            {
                MessageBox.Show("Keine Berechtigung zum Schreiben in dem Standardverzeichnis.\n" +
                    "Wenden Sie sich bitte an Ihren Administrator", "Schreibe-/Lese-Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Ein unbekannter Fehler ist aufgetreten.\n" +
                    "Wenden Sie sich bitte an Ihren Administrator", "Schreibe-/Lese-Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
       
        }

        /// <summary>
        /// Opens the "pattern.rin" file. If it doesn't exist, it will be created.
        /// </summary>
        private void Open()
        {
            Stream stream = null;
            try
            {
                stream = File.Open("pattern.rin", FileMode.Open);
                BinaryFormatter bFormatter = new BinaryFormatter();
                lbMTA.Items.Clear();
                lbMTA.Items.AddRange((string[])bFormatter.Deserialize(stream));
                lbPSP.Items.Clear();
                lbPSP.Items.AddRange((string[])bFormatter.Deserialize(stream));
                stream.Close();
            }
            catch 
            {
                Save();
            }
        }

        private void bMTADelete_Click(object sender, EventArgs e)
        {
            lbMTA.Items.RemoveAt(lbMTA.SelectedIndex);
            Save();
            if (lbMTA.Items.Count == 0)
            {
                bMTADelete.Enabled = false;
                bMTAEdit.Enabled = false;
            }
        }

        private void bImport_Click(object sender, EventArgs e)
        {

        }

        private void bOpen_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.FileName = "*.xls";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                tbPath.Text = openFileDialog1.FileName;
            }
        }

        private void bDisplay_Click(object sender, EventArgs e)
        {
            ext = new Extractor();
            ext.OpenFile(tbPath.Text);
            if (rbMTA.Checked)
            {
                MTA temp;
                if (rbMonth.Checked)
                {
                    temp = ext.CreateMTA(0);
                }
                else
                {
                    temp = ext.CreateMTA(1);
                }
                drawMTA(temp);
            }
            else
            {
                Task temp = ext.CreatePSP();
                drawPSP(temp);
            }
        }

        private void bMTAEdit_Click(object sender, EventArgs e)
        {
            editIndex = lbMTA.SelectedIndex;
            AddDialog dia = new AddDialog(AddDialog.MTA,lbMTA.Items[editIndex].ToString());
            dia.Add = EditMTA;
            dia.ShowDialog();
            Save();
        }

        private void bPSPEdit_Click(object sender, EventArgs e)
        {
            editIndex = lbPSP.SelectedIndex;
            AddDialog dia = new AddDialog(AddDialog.PSP, lbPSP.Items[editIndex].ToString());
            dia.Add = EditPSP;
            dia.ShowDialog();
            Save();
        }

        private void lbMTA_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lbMTA.SelectedIndex > -1)
            {
                bMTAEdit.Enabled = true;
                bMTADelete.Enabled = true;
            }
        }

        private void lbPSP_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lbPSP.SelectedIndex > -1)
            {
                bPSPEdit.Enabled = true;
                bPSPDelete.Enabled = true;
            }
        }

        private void bCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            Process.Start("help\\help\\help.html");
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            Process.Start("help\\help\\help.html");
        }
    }
}
